package ee.ut.aa.neuraltic.model;

import java.util.ArrayList;

import org.apache.log4j.Logger;

public class FirBoard implements Board {

	private static Logger log = Logger.getLogger( FirBoard.class );

	private int rowSize = 10;
	
	private int size = 100;
	private int[] fields = new int[size];

	private int nextPlayer = PLAYER_ONE;

	public FirBoard() {
		for (int i = 0; i < fields.length; i++) {
			fields[i] = 0;
		}
	}

	public FirBoard( int[] fields, int nextPlayer ) {

		this.fields = fields;
		this.size = fields.length;
		this.nextPlayer = nextPlayer;
	}

	@Override
	public int getSize() {

		return size;
	}

	@Override
	public int getValue( int index ) {

		int value = fields[index];

		log.debug( "Returning value " + value + " at index \t" + index );

		return value;
	}

	@Override
	public void setValue( int index, int value ) {

		log.debug( "Setting value " + value + " at index \t" + index );

		fields[index] = value;
	}

	@Override
	public void makeMove( int index ) {

		setValue( index, nextPlayer );

		// Switch nextPlayer;
		nextPlayer = nextPlayer == PLAYER_ONE ? PLAYER_TWO : PLAYER_ONE;
	}

	@Override
	public int[] getFields() {
		log.debug( "Returning board fields." );

		return fields;
	}

	@Override
	public void setFields( int[] fields ) {

		this.fields = fields;
	}

	@Override
	public int getNextPlayer() {
		return nextPlayer;
	}

	@Override
	public void setNextPlayer( int nextPlayer ) {
		this.nextPlayer = nextPlayer;
	}

	@Override
	public boolean isLegalMove( int index ) {

		boolean result = fields[index] == 0;

		log.debug( "Checking legal move for index " + index + ", result = " + result );

		return result;
	}

	@Override
	public ArrayList<Integer> getLegalMoves() {

		ArrayList<Integer> result = new ArrayList<Integer>();
		
		ArrayList<Integer> illegal = new ArrayList<Integer>();

		for( int i = 0; i < fields.length; i++ ) {

			if( fields[i] != 0 )
				illegal.add( i );
				
		}
		
		if( illegal.size() == 0 )
		{
			for( int i = 3; i < rowSize - 3; i++)
			{
				for( int j = 3; j< rowSize - 3; j++)
				{
					result.add(i*rowSize + j);
				}
			}
		}
		else 
		{
			for (Integer position : illegal) {
				int minY, minX;
				int maxY, maxX;
				
				int posX = position % rowSize;
				int posY = (position - posX) / rowSize;
								
				minY = posY - 1 >= 0 ? posY - 1 : 0;
				minX = posX - 1 >= 0 ? posX - 1 : 0;
				
				maxY = posY + 1 < rowSize ? posY + 1 : rowSize-1;
				maxX = posX + 1 < rowSize ? posX + 1 : rowSize-1;
				
				for( int i = minY; i <= maxY; i++)
				{
					for( int j = minX; j <= maxX; j++)
					{
						int move = i * rowSize + j;
						if( !illegal.contains(move) && !result.contains(move))
						{
							result.add(move);
						}
					}
				}
				
			}
		}

		log.debug( "Returning legal moves: " + result );

		return result;
	}

	@Override
	public String toString() {

		String result = "\n";
		
		for (int i = 0; i < fields.length; i++) {
			result += fields[i];			
			result += i%rowSize == rowSize-1 ? "\n" : "";
		}

		return result;
	}
}
